const { get_modules_of_dir } = require("../utils");
const bodyparser = require("koa-bodyparser");
const logger = require("koa-logger");
const json = require("koa-json");
const static = require("koa-static");

module.exports = {
  middlewares: {},

  async register(app) {
    const dir = `${__dirname}/modules`;
    const modules = await get_modules_of_dir(dir);
    modules.forEach((module) => (this.middlewares[module.name] = module));

    app.use(bodyparser({ enableTypes: ["json", "form", "text"] }));
    app.use(json());
    app.use(logger());
    app.use(static(`${process.cwd()}/public`));
    app.use(this.middlewares.cors.core);
    app.use(this.middlewares.req_logger.core);
    app.use(this.middlewares.jwt.core);
  },
};
